# encoding: utf-8

import os
import platform

import yaml

# 基础目录
BasePath = os.path.dirname(os.path.dirname(__file__))

# ex
ExtractDataPath = os.path.join(BasePath, 'data', 'extract.yaml')
if platform.system() == 'Linux':
	# 系统配置文件
	SysConfPath = os.path.join(BasePath, 'configs/prod.yaml')
else:
	SysConfPath = os.path.join(BasePath, 'configs/dev.yaml')
# 工具文件
Utils = os.path.join(BasePath, 'utils')
# 日志文件
LogFilePath = os.path.join(BasePath, 'logs')


# 读取yaml文件
class YamlReader:

	def __init__(self, read_yaml_file_path=None, write_yaml_file_path=None):
		if read_yaml_file_path is not None and os.path.exists(read_yaml_file_path):
			self.read_yaml_file_path = read_yaml_file_path
		elif write_yaml_file_path is not None:
			self.write_yaml_file_path = write_yaml_file_path
		else:
			raise FileNotFoundError("yaml文件未找到!")
		self._data = None

	@property
	def data(self):
		with open(self.read_yaml_file_path, 'rb') as f:
			self._data = yaml.safe_load(f)
		return self._data

	def get(self, key):
		return self.data.get(key)

	def set(self, key, value):
		data = dict()
		data[key] = value
		with open(self.write_yaml_file_path, 'a') as f:
			yaml.dump(data, f, allow_unicode=True)


# 读取系统配置
sysConf = YamlReader(SysConfPath)

# 写入yaml
writeYaml = YamlReader(write_yaml_file_path=ExtractDataPath)
